﻿Imports System.IO
Imports Training.Common

Partial Public Class EditQuestion
    Inherits System.Web.UI.Page
    Dim qsid As Integer ' questionid
    Dim ques As String ' question
    Dim qim As String ' questionimage
    Dim imp As Integer ' image placement
    Dim blnFull As Boolean ' boolean to check if full image upload succeeds
    Dim blnThmb As Boolean ' boolean to check if thumb image upload succeeds
    Dim blnUpld As Boolean ' boolean to determine if uploads are used
    Dim imgName As String ' image name of uploaded file

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' check for querystring data
        If IsNothing(Request.QueryString("qsid")) Then
            Response.Redirect("Questions.aspx")
        Else
            qsid = Request.QueryString("qsid")
        End If

        ' set initial value of image uploads
        blnUpld = False

        If Page.IsPostBack Then
            If cbImages.Checked Then
                ' enable upload control
                EnableUploads()
                blnUpld = True
            Else
                ' disable upload controls
                DisableUploads()
                blnUpld = False
            End If
        Else
            LoadVariables(qsid)
        End If
    End Sub

    ''' <summary>
    ''' load variables for question into control
    ''' </summary>
    ''' <param name="qsid"></param>
    ''' <remarks></remarks>
    Private Sub LoadVariables(ByVal qsid As Integer)
        ' get data for question
        Dim qArry As Array
        qArry = GetQuestionData(qsid)
        ' load variables from
        ques = qArry(0)
        If qArry(1) = String.Empty Then
            qim = "NONE"
        Else
            qim = qArry(1)
        End If

        If qArry(2) = String.Empty Then
            imp = -1
        Else
            imp = qArry(2)
        End If


        tbQuestion.Text = ques
        lblImage.Text = qim
        ddlPlacement.SelectedValue = imp
    End Sub

    ''' <summary>
    ''' Enable upload controls
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EnableUploads()
        rfFullImg.Enabled = True
        fuImage.Enabled = True
        rfThumbImg.Enabled = True
        fuThumb.Enabled = True
        rvPlace.Enabled = True
        ddlPlacement.Enabled = True
    End Sub

    ''' <summary>
    ''' Disable upload controls
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DisableUploads()
        rfFullImg.Enabled = False
        fuImage.Enabled = False
        rfThumbImg.Enabled = False
        fuThumb.Enabled = False
        rvPlace.Enabled = False
        ddlPlacement.Enabled = False
    End Sub

    ''' <summary>
    ''' Upload full image
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UploadFullFile()
        Dim fn As String ' file name
        Dim ct As String ' file content
        Dim fz As String ' file size
        Dim fileUploaded As Boolean

        ' initial value of fileUploaded
        fileUploaded = False

        fn = fuImage.PostedFile.FileName
        ct = fuImage.PostedFile.ContentType
        fz = fuImage.PostedFile.ContentLength

        ' define path to upload folder
        Dim p As String
        p = Server.MapPath("../images/quizzes/full")

        Try
            ' upload the actual file
            fuImage.PostedFile.SaveAs(p & "\" & System.IO.Path.GetFileName(fn.ToString()))
            ' set fileUploaded
            fileUploaded = True

            ' proceed with data insert if upload was successful
            If fileUploaded Then
                ' get file type and path
                Dim typ As String ' type of file
                Dim pth As String ' path to file
                typ = Right(fn, 4)
                pth = System.IO.Path.GetFileName(fn.ToString())

                ' determine type
                Select Case typ
                    Case ".gif"
                        typ = ".gif"
                    Case ".jpg"
                        typ = ".jpg"
                    Case ".png"
                        typ = ".png"
                    Case Else
                        lblMsg.Text = "Full Size image file type is not supported. Upload aborted."
                        Exit Sub
                End Select

                blnFull = True
                imgName = pth

            Else
                ' upload failed, notify user
                lblMsg.Text = "Full size file was not uploaded. Insert aborted."
                blnFull = False
            End If
        Catch ex As Exception
            ' also log error here
            lblMsg.Text = ex.ToString
        End Try

    End Sub

    ''' <summary>
    ''' Upload thumb file image
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UploadThumbFile()
        Dim fn As String ' file name
        Dim ct As String ' file content
        Dim fz As String ' file size
        Dim fileUploaded As Boolean

        ' initial value of fileUploaded
        fileUploaded = False

        fn = fuThumb.PostedFile.FileName
        ct = fuThumb.PostedFile.ContentType
        fz = fuThumb.PostedFile.ContentLength

        ' define path to upload folder
        Dim p As String
        p = Server.MapPath("../images/quizzes/thumb")

        Try
            ' upload the actual file
            fuThumb.PostedFile.SaveAs(p & "\" & System.IO.Path.GetFileName(fn.ToString()))
            ' set fileUploaded
            fileUploaded = True

            ' proceed with data insert if upload was successful
            If fileUploaded Then
                ' get file type and path
                Dim typ As String ' type of file
                Dim pth As String ' path to file
                typ = Right(fn, 4)
                pth = System.IO.Path.GetFileName(fn.ToString())

                ' determine type
                Select Case typ
                    Case ".gif"
                        typ = ".gif"
                    Case ".jpg"
                        typ = ".jpg"
                    Case ".png"
                        typ = ".png"
                    Case Else
                        lblMsg.Text = "Thumb Size image file type is not supported. Upload aborted."
                        Exit Sub
                End Select

                blnThmb = True

            Else
                ' upload failed, notify user
                lblMsg.Text = "Thumbnail file was not uploaded. Insert aborted."
                blnThmb = False
            End If
        Catch ex As Exception
            ' also log error here
            lblMsg.Text = ex.ToString
        End Try

    End Sub

    ''' <summary>
    ''' Insert question data
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UpdateQuestion()
        Dim ques As String ' question
        Dim place As Integer ' image placement

        ques = tbQuestion.Text

        If blnUpld Then
            ' check for successfull uploads before update
            If blnFull And blnThmb Then
                ' update with image information
                place = ddlPlacement.SelectedValue
                If UpdateQuestionData(qsid, ques, imgName, place) = 0 Then
                    lblMsg.Text = "Question <i>" & tbQuestion.Text & "</i> was updated."
                    ClearValues()
                Else
                    lblMsg.Text = "An error occured, question has NOT been inserted."
                End If
            Else
                lblMsg.Text = "An upload issue occured, insert was not successful."
            End If

        Else
            ' update WITHOUT image information
            If UpdateQuestionData(qsid, ques, "", 9) = 0 Then
                lblMsg.Text = "Question <i>" & tbQuestion.Text & "</i> was updated."
                ClearValues()
            Else
                lblMsg.Text = "An error occured, question has NOT been inserted."
            End If
        End If
    End Sub

    ''' <summary>
    ''' reset form values
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ClearValues()
        tbQuestion.Text = String.Empty
        ddlPlacement.SelectedValue = -1
    End Sub

    ''' <summary>
    ''' Edit button click event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        ' check if uploads should be called
        If blnUpld Then
            UploadFullFile()
            UploadThumbFile()
        End If
        UpdateQuestion()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        DeleteQuestionByID(qsid)
        lblMsg.Text = "Question has been deleted. All associated references have been removed."
    End Sub
End Class